<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">试卷批改</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>试卷批改</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            考卷名：
            <select id="examPaperName">
                <option value="">-请选择-</option>
            </select>&emsp;&emsp;
            状态：
            <select id="examPaperStatus">
                <option value="">-请选择-</option>
                <option value="1">未考试</option>
                <option value="2">已交卷</option>
                <option value="3">已完成</option>
            </select>&emsp;
            <button id="exam-check-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>
    </div>
    <div class="layui-card-body">
        <table class="layui-table" id="exam-check" lay-filter="exam-check-filter"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="exam-check-table-bar">
    <a class="layui-btn layui-btn-normal  layui-btn-xs " lay-event="exam-check">批改试卷</a>
    <a class="layui-btn layui-btn-primary  layui-btn-xs " lay-event="exam-view">查看结果</a>
</script>


<script>
    layui.use(['index', 'form', 'table', 'util', 'config', 'admin', 'layer'], function () {
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var index = layui.index;
        var layer = layui.layer;
        var form = layui.form;
        let userid;
        index.getUser(function (user) {
            userid = user.id
        });

        admin.req('api-exam/examPaper/findPaper?creator='+userid,{},function (res) {
           let data=res.data;
            for (let i = 0; i < data.length; i++) {
                $("#examPaperName").append('<option value="'+data[i].expaid+'">'+data[i].epname+'</option>')
            }
        },"get");
        // 渲染表格
        table.render({
            elem: '#exam-check',
            url: config.base_server + 'api-exam/exam/findSubmitExam',
            method: 'get',
            where: {
                access_token: config.getToken().access_token,
                teacid: userid
            },
            page: true,
            height: 'full-350',
            cols: [[
                {type: 'numbers'},
                {field: 'epname', title: '试卷名'},
                {field: 'remark', title: '简介要求'},
                {field: 'nickName', width: 120, title: '创建人'},
                {field: 'username', width: 120, title: '答题者'},
                {field: 'cretim', width: 120, title: '创建时间'},
                {
                    field: 'status', width: 100, title: '状态',
                    templet: function (data) {
                        let param = {expaid: data.expaid, userid: data.userid};
                        let result = getStatus(param);
                        if (result[0].status === 1) {
                            return '<span class="layui-badge layui-bg-gray">未考试</span>';
                        } else if (result[0].status === 2) {
                            return '<span class="layui-badge layui-bg-green">已交卷</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-gray">已完成</span>';
                        }
                    }
                },
                {align: 'center', toolbar: '#exam-check-table-bar', title: '操作', width: 250}
            ]],
            limit: 10,
        });

        table.on('tool(exam-check-filter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'exam-check') { // 批改试卷
                examCheck(data)
            }
            if (layEvent === 'exam-view') { // 查看结果
                examView(data)
            }
        });
        //批改试卷
        function examCheck(data) {
            let param = {userid: data.userid, expaid: data.expaid};
            let res = getStatus(param);
            if (res[0].status !=2) {
                layer.msg("不能进行此操作")
                return false;
            }
            admin.putTempData("paper_data", data);
            admin.popupCenter({
                title: '试卷：' + data.epname+"/考生："+data.username,
                area: ['50%', '75%'],
                path: 'pages/exam/exam-check-update.html',
                success: function (layero) {
                    $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                },
                finish: function () {
                    table.reload('exam-check');
                }
            });
        }
        //查看答案
        function examView(data) {
            let param = {userid: data.userid, expaid: data.expaid};
            let res = getStatus(param);
            if (res[0].status != 3) {
                layer.msg("请先批改试卷");
                return false;
            }
            admin.putTempData("paper_data", data);
            admin.popupCenter({
                title: '试卷：' + data.epname+"/考生："+data.username,
                area: ['50%', '75%'],
                path: 'pages/exam/exam-result.html',
                success: function (layero) {
                    $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                },
                finish: function () {
                    table.reload('exam-check');
                }
            });
        }

        //条件搜索
        $("#exam-check-btn-search").click(function () {
            let expaid = $("#examPaperName").val();
            let status = $("#examPaperStatus").val();
            let param = {expaid: expaid, status: status};
            table.reload('exam-check', {where: param});
        });

        //获取考试状态
        function getStatus(param) {
            let result;
            admin.req("api-exam/exam/findAnswer", param, function (res) {
                result = res.data;
            }, "get");
            return result;
        }

    });


</script>